//
//  WHBaseViewController.m
//  WHPersonalData
//
//  Created by shengmeng chen on 2019/4/19.
//  Copyright © 2019 mac. All rights reserved.
//

#import "WHBaseTableViewController.h"
//#import "MJRefresh.h"
//#import "SVProgressHUD.h"
#import "WHNoCellLineTableView.h"
#import "SearchTitleView.h"

@interface WHBaseTableViewController ()
<
UITableViewDelegate,UITableViewDataSource
,SearchTitleViewDelegate>

@end

@implementation WHBaseTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self baseSetting];
    /*
     当translucent = YES，controller中self.view的原点是从导航栏左上角开始计算
     当translucent = NO，controller中self.view的原点是从导航栏左下角开始计算
     */
    self.navigationController.navigationBar.translucent = YES;
    self.isHaveMoreData   = YES;
    self.currentPageIndex = 0;
    self.datas = [NSMutableArray new];
    [self setupUI];
    [self loadDatas];
    
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
//    if ([SVProgressHUD isVisible]) {
//        [SVProgressHUD dismiss];
//    }
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    if ([SVProgressHUD isVisible]) {
//        [SVProgressHUD dismiss];
//    }
    //取消当前控制器的网络请求
    //  [YHAFNHelper mn_cancelRequet];
}

-(void)setDatas:(NSMutableArray *)datas
{  _datas = datas;
   [self.tableView reloadData];
}

#pragma mark - baseSetting
- (void)baseSetting{
    self.view.backgroundColor = [UIColor whiteColor];

}

#pragma mark - loadDatas
- (void)loadDatas{
  
}

- (void)loadMoreDatas{
    
    //[_tableView.mj_footer endRefreshingWithNoMoreData];
}

- (void)refreshData{

}

#pragma mark - setupUI
- (void)setupUI{
    
    //子类继承一定要记得super！不然tableView没了！！
    [self createTableViewWithStyle:UITableViewStyleGrouped];
    
    //上拉加载更多
   // [self p_addMJRefshControls];
}

//UITableViewStylePlain 类型tableView
- (void)createTableViewStyleGrouped{
    
    [self createTableViewWithStyle:UITableViewStyleGrouped];
}

//UITableViewStylePlain 类型tableView
- (void)createTableViewStylePlain{
    
    [self createTableViewWithStyle:UITableViewStylePlain];
}

- (void)createTableViewWithStyle:(UITableViewStyle)style{
    
    WHNoCellLineTableView*tableView = [[WHNoCellLineTableView alloc]initWithFrame:Frame(0,navHeight1, ScreenW, ScreenH - navHeight1) style:style];
    
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.estimatedRowHeight = 0;
    tableView.estimatedSectionFooterHeight = 0;
    tableView.estimatedSectionHeaderHeight = 0;
    [self.view addSubview:tableView];
    _tableView = tableView;
    
    //[self p_addMJRefshControls];
}

- (void)mn_fixTableViewHeightWithTableView:(UITableView *)tableView{
    tableView.frame = Frame(0, navHeight1, ScreenW, ScreenH - navHeight1 - DefaultBottomTabBarHeight);
}

#pragma mark - MJRefresh
//- (void)p_addMJRefshControls{
//    //上拉加载更多
//    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreDatas)];
//    _tableView.mj_footer.hidden = YES;
//    
//    //下拉刷新
//    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
//    _tableView.mj_header.hidden = YES;
//}

- (void)receiveNotis{
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [UITableViewCell new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

@end

/**没有section的tableVIew**/
@implementation WHBaseControllerTypeNoSection

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}
@end

/**有section的tableVIew**/
@implementation WHBaseControllerTypeHadSection

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.datas.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.datas[section] count];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

@end
/*数据结构是 - @[],没有section的,有搜索的 tableVIew*/
@implementation WHBaseControllerTypeNoSectionHaveSearch

-(void)setupUI
{
    [super setupUI];
    self.tableView.frame =CGRectMake(0,navHeight1+40,ScreenW,ScreenH-navHeight1-40);
    SearchTitleView*searchTitleView = [SearchTitleView titleView];
    searchTitleView.returnKeyType = UIReturnKeySearch;
    searchTitleView.delegate = self;
    self.searchView = searchTitleView;
    [self.view addSubview:searchTitleView];
}

@end

